#include "Dialog.h"
#include <SEUi/ImGui/imgui.h>

namespace SEngine
{
    Dialog::Dialog()
    {
        m_open = false;
    }
    
    void Dialog::Update(int delta)
    {
        BeginUpdate();
        if (m_open) {
            ImGui::OpenPopup(m_name.c_str());
        }
        if (m_type == DialogType::Modal ? ImGui::BeginPopupModal(m_name.c_str(), &m_open, m_windowFlags) : ImGui::BeginPopup(m_name.c_str(), m_windowFlags)) {
            if (!m_open) SignalClose.Invoke();
            m_isFocused = ImGui::IsWindowHovered();
            auto size = ImGui::GetWindowSize();
            auto pos = ImGui::GetWindowPos();
            m_rect = glm::ivec4((int)pos.x, (int)pos.y, (int)size.x, (int)size.y);
            UpdateImpl(delta);
            EnableEventHandle::OnUpdateImpl(delta);
            ImGui::EndPopup();
        }
        EndUpdate();
    }

    void Dialog::Popup()
    {
        m_open = true;
    }
}